import { createWebHistory, createRouter, RouteRecordRaw } from "vue-router";
import { getToken } from "../utils/cache";
import User from "../layout/User.vue";

const routes: RouteRecordRaw[] = [
  { path: "/", component: () => import("../views/Home.vue") },
  // { path: "/product/:id", component: () => import("../views/Product.vue") },
  // { path: "/homeList", component: () => import("../views/HomeList.vue") },

  { path: "/login", component: () => import("../views/Login.vue") },
  {
    path: "/UserHome",
    component: User,
    children: [
      // {
      //   path: "/input",
      //   component: () => import("../views/Input.vue"),
      //   meta: { auth: true },
      // },
      {
        path: "/inputList",
        component: () => import("../views/InputList.vue"),
        meta: { auth: true },
      },
      {
        path: "/data",
        component: () => import("../views/Data.vue"),
        meta: { auth: true },
      },{
        path: "/userlist",
        component: () => import("../views/UserList.vue"),
        meta: { auth: true },
      },{
        path: "/adduser",
        component: () => import("../views/AddUser.vue"),
        meta: { auth: true },
      },
    ],
  },
  { path: "/:pathMatch(.*)", redirect: "/", name: "notMatch" },
];

export const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_BASE_PATH),
  routes,
});

router.beforeEach((to, from, next) => {
  console.log(to, from);
  if (to.meta.auth && !getToken()) {
    next("/login?redirect=" + to.fullPath);
  } else {
    next();
  }
});
